package com.arron.algorithm.leetcodetop100.数组.矩阵;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class 螺旋矩阵II {




    public int[][] generateMatrix(int n) {

        int up_bound = 0;
        int down_bound = n-1;
        int left_bound = 0;
        int right_bound = n-1;



        int[][] res = new int[n][n];
        int count = 1;
        while (up_bound <= down_bound && left_bound<=right_bound) {

            //上边，从左到右遍历
            for (int i = left_bound; i <= right_bound; i++) {

                res[up_bound][i] = count;
                count++;
            }
            up_bound++;
            if (up_bound>down_bound){
                break;
            }
            // 右边 从上到下
            for (int i = up_bound; i <= down_bound; i++) {
                res[i][right_bound] = count;
                count++;
            }
            right_bound--;
            if (left_bound>right_bound){
                break;
            }
            // 下边 从右到左
            for (int i = right_bound; i >= left_bound; i--) {
                res[down_bound][i] = count;
                count++;
            }
            down_bound--;
            if (up_bound>down_bound){
                break;
            }
            // 左边 从下到上
            for (int i = down_bound; i >= up_bound; i--) {
                res[i][left_bound] = count;
                count++;
            }
            left_bound++;
        }

        return res;

    }


    public static void main(String[] args) {
        螺旋矩阵II ii = new 螺旋矩阵II();

        int[][] ints = ii.generateMatrix(3);
        for (int[] anInt : ints) {
            System.out.println(Arrays.toString(anInt));
        }



    }


}
